package com.zq.sysuser.service.impl;

import cn.dev33.satoken.stp.StpInterface;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zq.sysuser.dao.entity.Permission;
import com.zq.sysuser.dao.entity.Role;
import com.zq.sysuser.dao.entity.RolePermission;
import com.zq.sysuser.dao.entity.UserRole;
import com.zq.sysuser.service.IPermissionService;
import com.zq.sysuser.service.IRolePermissionService;
import com.zq.sysuser.service.IRoleService;
import com.zq.sysuser.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author 张乔
 * @Date 2024/4/27 16:10
 */
@Service
public class StpInterfaceImpl implements StpInterface {
    //    用户角色表
    @Autowired
    private IUserRoleService sysUserRoleService;
    //角色权限表
    @Autowired
    private IRolePermissionService sysRoleMenuService;
    //    权限表
    @Autowired
    private IPermissionService sysMenuService;
//    角色表
    @Autowired
    private IRoleService sysRoleService;

@Autowired
private RedisTemplate<String,String> redisTemplate;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
//        先从redis中查询用户权限，查不到再从数据库查
        String redisPermissionList = redisTemplate.opsForValue().get("permissionList" + loginId);
        if (redisPermissionList!=null){
            // 返回此 loginId 拥有的权限列表
            return JSON.parseArray(redisPermissionList, String.class);
        }


        // 返回此 loginId 拥有的权限列表
        List<UserRole> sysUserRoles = sysUserRoleService.list(new LambdaQueryWrapper<UserRole>().
                eq(UserRole::getUserId, Integer.parseInt(loginId.toString())));
//        得到角色id集合
        List<Integer> roleIds = sysUserRoles.stream().map(UserRole::getRoleId).toList();
        if (roleIds.size()== 0){
            return null;
        }

        List<String> list=new ArrayList<>();
//    根据角色id查权限id
        roleIds.forEach(roleId -> {
            List<RolePermission> roleMenus = sysRoleMenuService.list(new LambdaQueryWrapper<RolePermission>().
                    eq(RolePermission::getRoleId, roleId));
            List<Integer> mends = roleMenus.stream().map(RolePermission::getPermissionId).toList();

            mends.forEach(menuId ->{
                Permission sysMenu = sysMenuService.getById(menuId);
                list.add(sysMenu.getName());
            });
        });
//     将查询到的权限放到redis中，便于下次的直接使用
        redisTemplate.opsForValue().set("permissionList:"+loginId,list.toString(),30, TimeUnit.MINUTES);

        // 返回此 loginId 拥有的权限列表
        return list;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        //        先从redis中查询用户权限，查不到再从数据库查
        String redisPermissionList = redisTemplate.opsForValue().get("roleList" + loginId);
        if (redisPermissionList!=null){
            // 返回此 loginId 拥有的权限列表
            return JSON.parseArray(redisPermissionList, String.class);
        }
        // 返回此 loginId 拥有的角色列表
        List<UserRole> sysUserRoles = sysUserRoleService.list(new LambdaQueryWrapper<UserRole>().
                eq(UserRole::getUserId, Integer.parseInt(loginId.toString())));
//        得到角色id集合
        List<Integer> roleIds = sysUserRoles.stream().map(UserRole::getRoleId).toList();
        if (roleIds.size()== 0){
            return null;
        }
        List<String> list=new ArrayList<>();
        roleIds.forEach(roleId->{
            Role role = sysRoleService.getById(roleId);
            list.add(role.getName());
        });
        redisTemplate.opsForValue().set("roleList:"+loginId,list.toString(),30, TimeUnit.MINUTES);

        // 返回此 loginId 拥有的角色列表
        return list;
    }
}
